package com.example.activity;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import com.example.model.HistoryModel;
import com.example.model.LocationListAdapter;
import com.example.model.LocationModel;
import com.example.test.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	private LocationModel locModel;
	private HistoryModel hisModel;
	private Cursor mCursor;

	private EditText locName;
	private EditText currentLoc;
	private ListView locationList;

	private int Location_ID = 0;
	private String currentLocation;

	private String locname, currentloc;

	private View itemView;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		locModel = new LocationModel(this);
		mCursor = locModel.select();

		hisModel = new HistoryModel(this);

		locName = (EditText) findViewById(R.id.locationname);
		currentLoc = (EditText) findViewById(R.id.curloc_set);
		locationList = (ListView) findViewById(R.id.bookslist);

		locname = locName.getText().toString();

		Intent addloc = getIntent();
		Bundle bundle = addloc.getExtras();
		currentLocation = bundle.getString("CurrentLocation");
		currentLoc.setText(currentLocation);

		locationList.setAdapter(new LocationListAdapter(this, mCursor));

		Button add = (Button) findViewById(R.id.btn_add_loc);
		add.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

				if (!"".equals(locName.getText().toString())) {

					add();

				} else {
					showDialog();
				}

			}

		});
		locationList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view,
					int position, long id) {
				itemBackChanged(view);
				mCursor.moveToPosition(position);
				Location_ID = mCursor.getInt(0);
				locName.setText(mCursor.getString(1));
				currentLoc.setText(mCursor.getString(2));

			}

			private void itemBackChanged(View view) {

				view.setBackgroundResource(android.R.color.holo_red_light);

				itemView = view;

			}

		});
		Button delete = (Button) findViewById(R.id.btn_delete_loc);
		delete.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				delete();
			}

		});
		Button go = (Button) findViewById(R.id.btn_go);
		go.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (!"".equals(locName.getText().toString())) {
					
					String urlb = query();
					if (urlb !=null) {
						System.out.print("urllllllllllllllllllllllll");// test
						String curLoc = currentLoc.getText().toString();

					/*	Calendar c = Calendar.getInstance();
						SimpleDateFormat tf = new SimpleDateFormat("H");
						String timef = tf.format(c.getTime());
						int t = Integer.parseInt(timef);
						String timeFrame = null;
						if (t >= 6 && t <= 8)

							timeFrame = "Morning";

						else if (t >= 9 && t < 11)
							timeFrame = "Antemeridiem";
						else if (t >= 11 && t < 13)
							timeFrame = "LunchTime";
						else if (t >= 13 && t < 17)
							timeFrame = "Afternoon";
						else if (t >= 17 && t < 19)
							timeFrame = "DinnerTime";
						else if (t >= 1 && t < 23)
							timeFrame = "Evening";
						else
							timeFrame = "Midnight";*/

						Intent intent = new Intent(MainActivity.this,
								BrowserView.class);
						Bundle bundle = new Bundle();
						bundle.putString("CURLOC", curLoc);
						bundle.putString("URL", urlb);
						intent.putExtras(bundle);
						startActivity(intent);
					} else {
						System.out.print("nonononononononononononon");// test
						String curLoc = mCursor.getString(2);
						Intent intent = new Intent(MainActivity.this,
								BrowserView.class);
						Bundle bundle = new Bundle();
						bundle.putString("CURLOC", curLoc);

						intent.putExtras(bundle);
						startActivity(intent);

					}
				} else {
					showDialog();
				}

			}

		});

	}

	public void add() {

		locname = locName.getText().toString();
		currentloc = currentLoc.getText().toString();
		if (locname == null) {
			Toast.makeText(this, "Please enter the Location Name!!",
					Toast.LENGTH_LONG).show();

		}

		String currentlocation = locModel.query(locname);
		if (currentlocation == null) {
			locModel.insert(locname, currentloc);
			mCursor.requery();
			locationList.invalidateViews();
			locName.setText("");
			currentLoc.setText("");
			Toast.makeText(this, "Add Successed!", Toast.LENGTH_SHORT).show();
			return;
		}
		Toast.makeText(this,
				"You have had this Locaiton Information! Please set it again!",
				Toast.LENGTH_SHORT).show();
		locName.setText("");
	}

	public void delete() {
		locModel.delete(Location_ID);
		mCursor.requery();
		locationList.invalidateViews();
		locName.setText("");
		currentLoc.setText("");
		Toast.makeText(this, "Delete Successed!", Toast.LENGTH_SHORT).show();

	}

	public String query() {
		String curLoc = mCursor.getString(2);

		Calendar c = Calendar.getInstance();
		SimpleDateFormat tf = new SimpleDateFormat("H");
		String timef = tf.format(c.getTime());
		int t = Integer.parseInt(timef);
		String timeFrame = null;
		if (t >= 6 && t <= 8)

			timeFrame = "Morning";

		else if (t >= 9 && t < 11)
			timeFrame = "Antemeridiem";
		else if (t >= 11 && t < 13)
			timeFrame = "LunchTime";
		else if (t >= 13 && t < 17)
			timeFrame = "Afternoon";
		else if (t >= 17 && t < 19)
			timeFrame = "DinnerTime";
		else if (t >= 1 && t < 23)
			timeFrame = "Evening";
		else
			timeFrame = "Midnight";

		String url = hisModel.query_url(timeFrame, curLoc);
		System.out.printf("^^^^^^^^url", url);
		if (url == null) {
			Toast.makeText(this, "nonono!!", Toast.LENGTH_SHORT);
			return null;
		} else {
			Toast.makeText(this, "^^^^" + url, Toast.LENGTH_SHORT).show();
			return url;
		}
	}

	private void showDialog() {
		new AlertDialog.Builder(MainActivity.this).setTitle("Sorry!!!")
				.setMessage("Pleas Input the LocationName")
				.setPositiveButton("OK", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						Log.d("WebWiew", "Press the OK Button");
					}

				}).show();

	}

}